package com.itheima.web.controller.ums;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.ums.RoleService;
import com.itheima.vo.ums.UmsMenu;
import com.itheima.vo.ums.UmsResource;
import com.itheima.vo.ums.UmsRole;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @DubboReference
    private RoleService roleService;

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("listAll")
    public ResultData listAll(){
        List<UmsRole> umsRoles = roleService.list();
        return ResultData.ok("操作成功",umsRoles);
    }

    /**
     * 根据角色名称分页获取角色列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("list")
    public ResultData list(Integer pageNum,Integer pageSize,String keyword){
        Page<UmsRole> rolePage = roleService.getPage(pageNum,pageSize,keyword);

        PageResult page = PageResult.getPage(rolePage);

        return ResultData.ok("操作成功",page);
    }

    /**
     * 添加角色
     * @param umsRole
     * @return
     */
    @PostMapping("create")
    public ResultData create(@RequestBody UmsRole umsRole){
        roleService.save(umsRole);
        return ResultData.ok("操作成功",1);
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ResultData delete(@RequestParam Integer[] ids){
        Integer code = roleService.deleteById(ids);
        if(code == 0){
            return ResultData.error("操作失败,角色已绑定用户");
        }
        return ResultData.ok("操作成功",ids.length);
    }

    /**
     * 修改角色
     * @param id
     * @param umsRole
     * @return
     */
    @PostMapping("update/{id}")
    public ResultData update(@PathVariable Integer id,@RequestBody UmsRole umsRole){
        roleService.updateById(umsRole);
        return ResultData.ok("操作成功",1);
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("updateStatus/{id}")
    public ResultData updateStatus(@PathVariable Integer id,Integer status){
        roleService.updateStatus(id,status);
        return ResultData.ok("操作成功",null);
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @GetMapping("listMenu/{roleId}")
    public ResultData listMenu(@PathVariable Integer roleId){
        List<UmsMenu> menus = roleService.listMenu(roleId);
        return ResultData.ok("操作成功",menus);
    }

    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @GetMapping("listResource/{roleId}")
    public ResultData listResource(@PathVariable Integer roleId){
        List<UmsResource> resources = roleService.listResource(roleId);
        return ResultData.ok("操作成功",resources);
    }

    /**
     * 给角色分配菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("allocMenu")
    public ResultData allocMenu(Integer roleId,Integer[] menuIds){
        roleService.allocMenu(roleId,menuIds);
        return ResultData.ok("操作成功",menuIds.length);
    }

    /**
     * 给角色分配资源
     * @param roleId
     * @param resourceIds
     * @return
     */
    @PostMapping("allocResource")
    public ResultData allocRescource(Integer roleId,Integer[] resourceIds){
        roleService.allocRescource(roleId,resourceIds);
        return ResultData.ok("操作成功",resourceIds.length);
    }
}
